package fr.sharpe.app.earthvisualizationproject.controller;

import java.util.HashMap;
import java.util.Set;

import fr.sharpe.app.earthvisualizationproject.model.EarthLayer;
import fr.sharpe.app.earthvisualizationproject.ui.Earth;

public class CacheLayerController {

	private static CacheLayerController cache = null;
	private HashMap<String, EarthLayer> layers;
	
	
	public CacheLayerController()
	{
		layers = new HashMap<String, EarthLayer>();
		for(String s:Earth.getInstance().getLayersInit().keySet())
		{
			this.layers.put(s,null);
		}
	}
	
	public static CacheLayerController getInstance()
	{
		if(cache == null)
		{
			cache = new CacheLayerController();
		}
		return cache;
	}
	
	public EarthLayer findSelectedObject(Object o)
	{
		for(String s:this.layers.keySet())
		{
			if(this.layers.get(s) != null)
			{
				System.out.println(this.layers.get(s).getType()+ " = " + o.getClass());
				if(this.layers.get(s).getType().equals(o.getClass()))
				{
					if(this.layers.get(s).getConvertedObjects().containsValue(o))
					{
						System.out.println("FOUND IT");
						return this.layers.get(s);
					}
				}
			}
		}
		return null;
	}
	
	public boolean containsKey(String layerName)
	{
		return this.layers.containsKey(layerName);
	}
	
	public void addLayer(String layerName,Object type)
	{
		if(!this.layers.containsKey(layerName))
		{
			this.layers.put(layerName, new EarthLayer(layerName, type));
		}
	}
	
	public EarthLayer getLayer(String layerName)
	{
		return this.layers.get(layerName);
	}
	
	public void removeLayer(String layername)
	{
		this.layers.remove(layername);
	}
	
	public long numberOfLayers()
	{
		return this.layers.size();
	}
	
	public Set<String> keySet()
	{
		return this.layers.keySet();
	}
	
	
}
